Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
nodejs [] operator, avoid picking first char of array of strings

I have an array of strings that sometimes is an array that only contains one string. when that is the case array[0] does not return the whole string but returns the first char in the string.

I can get around it with some if checking if length is > 1 but it is awful.

is there a safe [] operator or method that always return the whole string in position 0 of the array instead of the first char in the string?

This is an express input-based array in an html form. I don't really have too much control over how it's generated.

i can't use a foreach as I have other parallel arrays from that form.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Before operating your var as an array, you should verify that it is an array.

In you case, i guess that when you get only a string the provider of your data return a string type and when there is more than one string it return an array of string.

So you need to verify the type returned by your data provider and then process with the right type.

if ( Array.isArray(yourVar) ) {
  console.log('My var is an array');
} else {
  console.log('My var is not an array');
}

You can also use typeof to check your var type, for example verify that your var is a string.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!